<?php
namespace app\system\validate;
use think\Validate;
use think\Db;
use \app\common\model\ConfigSet;


class Message extends Validate{
    use \app\traits\tablename\Member;
    // 验证规则
    protected $rule = [
       ['modular_type','require|integer|check_modular_type','-11762|-11762|-11763'],
       ['msg_type','require','-11760'],
       ['template_id','require|check_tem_id','-11764|-11765'],
       ['push_uid','require|check_uid','-11766|-11766'],
    ];

    protected $scene = [
        'check_modular_type'=> ['modular_type','msg_type','push_uid']
    ];

    //检测模板类型
    protected function check_modular_type($value,$rule,$data){

        /*$mall_domain = \app\common\model\Base::get_domain_set('mall_domain',1);
        $url = $mall_domain.'/api/wms_message/get_modular_type';
        $output = \app\common\model\Base::curl_post($url,[]);
        $output = json_decode( json_encode($output),true);
        if($output['code']>0 && in_array($value,array_keys($output['data']))){
            return true;
        }
        return -11763;*/
        $is_exist = db::connect('database.db_config2')->name('sms_template')->where(['type'=>$value,'is_delete'=>0])->find();
        return !empty($is_exist)?true:-11763;
    }

    protected function check_tem_id($value,$rule,$data){
        $sms_temp =   new ConfigSet();
        $tem_list = $sms_temp->get_sms_temp("is_delete=0 and id=".$value);
        return !empty($tem_list)?true:-11765;
    }

    protected function check_uid($value,$rule,$data){
        if(!empty($value)){
            $is_exit = db::name(self::$member_tb['member'])->where(['uid'=>$value])->find();
            return !empty($is_exit)?true:-10754;
        }elseif($value == 0){
            return true;
        }else{
            return false;
        }

    }
}